/*----------------------------------------------------------\\
|| Mancala                                                  ||
|| CS-340 with Professor Troy                               ||
|| Joshua Jung and Eugene Magdel                            ||
|| April 29, 2011                                           ||
\\----------------------------------------------------------*/
#ifndef MANCALA_H
#define MANCALA_H

#include <QMainWindow>
#include <QtNetwork>
#include <QGraphicsScene>
#include <QPushButton>
#include <QTimer>

#include "board.h"
#include "highscore.h"
#include "mancala_ai.h"

namespace Ui {
    class Mancala;
}

class Mancala : public QMainWindow
{
    Q_OBJECT

public:
    enum {GT_UNDEFINED, GT_LOCAL, GT_NETWORK, GT_AI};

    explicit Mancala(QWidget *parent = 0);
    ~Mancala();

    void initialize();
    void disconnectAll();
    void resetAll();
    void setupGameBoard();
    void startHostDrivenNetworkGame();

    void renderCupGemsInBounds(Cup *c, int x, int y, int w, int h);
    void checkWin();

    void writeByteImmediate(qint8 b);
    void setIsOurTurn(bool is);
private:
    // --------------------------------
    // UI
    // --------------------------------
    Ui::Mancala *ui;

    QPushButton **buttonsByIndex;
    QTimer *timer;

    // --------------------------------
    // GAME
    // --------------------------------
    int gameType;

    // The actual mancala game board data structure.

    Board *board;
    MancalaAi *ai_computer;
    bool isOurTurn;

    bool highscoreRequested;
    bool playing;
    bool haveWinner;

    // This variable makes sure the AI doesn't instantly take its turn
    // after the user clicks.
    int aiDelay;
    // --------------------------------
    // GRAPHICS
    // --------------------------------
    QGraphicsScene *graphicsScene;
    QPixmap *pm_gem;

    // --------------------------------
    // Network
    // --------------------------------
    bool hosting;
    bool connected;

    QTcpSocket *socket;
    QTcpServer *server;

private slots:
    void renderCups();
    void slot_move();
    void slot_hostJoinGame();
    void slot_newConnection();
    void slot_clientDisconnected();
    void slot_readReady();
    void slot_startNetworkGame();
    void slot_newLocalGame2Player();
    void slot_quit();
    void slot_highscores();
    void slot_local_ai();
    void slot_getHighScore();
    void slot_highscoreSave();
    void slot_help();
    void slot_about();
};

#endif // MANCALA_H
